if not pattern then pattern = {} end

if not pattern.press then

dofile (ScriptsRootPath .. "lib\\lib.lua")

pattern.press = {}

pattern.press.patterns_init = lib.path.init .. "patterns.ini"

function pattern.press.OnCandle(source, index, data)
	if not source then return end
	if not pattern.press.params then
		pattern.press.params = lib:LoadIni(pattern.press.patterns_init, "press", {
			base_candles = 4,    	-- number of candles before the last one
			atr_growth = 7.0,     	-- of base ATR
			vol_growth = 5.0,     	-- of base average volume
		})
		if processor and processor.log then
			processor:log("pattern.press.params", pattern.press.params)
		end
	end
	
	if not index then index = 1 end
	
	local candle = lib:GetLastCandle(source, index)
	local before = lib:GetLastCandle(source, index + 1)
	
	if not (candle and candle.instrument and before and lib:GetLastCandle(source, index + pattern.press.params.base_candles)) then return end
	
	local candle_size = candle.close - candle.open
	
	if math.abs(candle_size) * 1.5 < candle.high - candle.low then return end
	
	
	local base_volume = 0
	local base_atr = 0
	local c
	
	for i = 1, pattern.press.params.base_candles do
		c = lib:GetLastCandle(source, index + i)
		base_volume = base_volume + c.volume
		base_atr = base_atr + math.abs(c.close - c.open)
	end
	base_volume = base_volume / pattern.press.params.base_candles
	base_atr = base_atr / pattern.press.params.base_candles
	
	if base_volume >= candle.volume / pattern.press.params.vol_growth then return end
	if base_atr >= math.abs(candle_size) / pattern.press.params.atr_growth then return end
	
		
	local price = candle.low
	if candle_size >= 0 then price = candle.high end
	
	candle.datetime.is_long__ = candle_size >= 0
	
	if candle.datetime.is_long__ then
		if math.min(before.open, before.close) >= candle.open or before.high < candle.low then return end
	else
		if math.max(before.open, before.close) <= candle.open or before.low > candle.high then return end
	end
		
	candle.datetime.hint__ = lib:FormatPrice(price, candle.instrument)
			
	candle.datetime.notify__ = string.format("%s PRESS %s",
		candle.instrument.code,
		lib:FormatPrice(price, candle.instrument)
	)
	
	return price, candle.datetime
end

function TR(source, index) --True Range ("TR")
	index = index or 1
	
	local candle1 = lib:GetLastCandle(source, index)
	local candle2 = lib:GetLastCandle(source, index + 1)
	
	if candle1 and candle2 then
		return math.max(candle1.high - candle1.low, 
			math.abs(candle1.high - candle2.close), 
			math.abs(candle1.low - candle2.close))
	end
	
	return nil
end

function ATR(source, index) --Average True Range ("ATR")
	index = index or 1
	
	local period = 14
	local ret = 0
	local r
	
	for i = 0, period - 1 do
		r = TR(source, index + i)
		if not r then
			period = i
			break
		end
		ret = ret + r
	end
	
	if period > 0 then
		ret = ret / period
	end
	
	return ret
end

end -- of file
